//
//  STTabbarViewController.m
//  赛拓
//
//  Created by 王磊 on 7/28/16.
//  Copyright © 2016 立金所. All rights reserved.
//

#import "STTabbarViewController.h"
#import "STNavigationController.h"
#import "STOrderViewController.h"
#import "STUserViewController.h"
#import "STSellerViewController.h"
#import "STHomeViewController.h"
#import "AppDelegate.h"
#import "STLoginViewController.h"
@interface STTabbarViewController ()

@end

@implementation STTabbarViewController
+ (instancetype)instance {
    
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    if ([delegate.window.rootViewController isKindOfClass:[STTabbarViewController class]]) {
        
        return (STTabbarViewController *)delegate.window.rootViewController;
    } else {
        
        return [[self alloc]init];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ST_666666_COLOR;
    attrs[NSFontAttributeName] = ST_13_FONT;
    
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSFontAttributeName] = ST_13_FONT;
    
    selectedAttrs[NSForegroundColorAttributeName] = ST_666666_COLOR;
    
    UITabBarItem *item = [UITabBarItem appearance];
    
    [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    //
    [self setupChildVC:[STHomeViewController instance] title:@"首页" image:@"首页-1" selectedImage:@"首页-1-拷贝"];
    
    [self setupChildVC:[STSellerViewController instance] title:@"商家" image:@"商品" selectedImage:@"商品-拷贝"];
    
    [self setupChildVC:[STOrderViewController instance] title:@"订单" image:@"icon(订单)" selectedImage:@"icon(订单)-拷贝"];
    
    [self setupChildVC:[STUserViewController instance] title:@"用户" image:@"用户" selectedImage:@"用户-拷贝"];
    
    //        [[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:HZS_Tabbar]];
    
    [STNotiCenter addObserver:self selector:@selector(logout) name:ST_Logout_Noti object:nil];
}
- (void)logout {
    
    UIWindow *keyWindow = [UIApplication sharedApplication].delegate.window;
    
    if (![keyWindow.rootViewController isKindOfClass:[STNavigationController class]]) {
        
        keyWindow.rootViewController = [[STNavigationController alloc]initWithRootViewController:[STLoginViewController instance]];
        
    } else {
        
        [self dismissViewControllerAnimated:true completion:nil];
        
    }
    
    
}

- (void )setupChildVC:(UIViewController*)vc title:(NSString*)title image:(NSString *)image selectedImage:(NSString *)selectedImage {
    // 设置文字和图片
    vc.navigationItem.title = title;
    
    vc.tabBarItem.title = title;
    
    vc.title = title;
    
    vc.tabBarItem.image = [[UIImage imageNamed:image]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    STNavigationController *nav=[[STNavigationController alloc]initWithRootViewController:vc];
    
    [self addChildViewController:nav];
}


@end
